package com.ssh.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Product;
import com.ssh.entity.Seller;
import com.ssh.manager.SellerManager;

@Service
public class SellerManagerImpl implements SellerManager {
	@Resource
	private IDao daoImpl;

	@Override
	public List<Seller> getAll() {
		String sql = "from Seller where del!=1 order by createDate desc";
		List<Seller> sellers = (List<Seller>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, -1, -1);
		return sellers;
	}

	@Override
	public Seller saveOrUpdateSeller(Seller seller) {
		return daoImpl.saveOrUpdateDomain(seller);
	}

	@Override
	public Seller getSellerById(Long id) {
		return daoImpl.getDomain(Seller.class, id);
	}

	@Override
	public boolean deleteSeller(Long sellerId) {
		try {
			Seller seller = daoImpl.getDomain(Seller.class, sellerId);
			seller.setDel(1);
			seller = daoImpl.saveOrUpdateDomain(seller);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public void findSellersByPage(Map conditionMap, Page page, String isDesc) {
		try {
			String sql = "select count(*) from Seller where del!=1";
			String findSql = "from Seller where del!=1";
			if(conditionMap.get("sellerName")!=null && !conditionMap.get("sellerName").equals("")){
				sql += " and name like '%"+conditionMap.get("sellerName")+"%'";
				findSql += " and name like '%"+conditionMap.get("sellerName")+"%'";
			}
			if(conditionMap.get("sellerJobNumber")!=null && !conditionMap.get("sellerJobNumber").equals("")){
				sql += " and jobNumber like '%"+conditionMap.get("sellerJobNumber")+"%'";
				findSql += " and jobNumber like '%"+conditionMap.get("sellerJobNumber")+"%'";
			}
			if(conditionMap.get("sellerPost")!=null && !conditionMap.get("sellerPost").equals("")){
				sql += " and post like '%"+conditionMap.get("sellerPost")+"%'";
				findSql += " and post like '%"+conditionMap.get("sellerPost")+"%'";
			}
			if(conditionMap.get("superMarket")!=null && !conditionMap.get("superMarket").equals("")){
				sql += " and superMarket.id="+conditionMap.get("superMarket");
				findSql += " and superMarket.id="+conditionMap.get("superMarket");
			}
			
			int sum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(sum));
			page.countTotalPageNum();
			
			findSql += " order by createDate "+isDesc;
			List<Seller> sellers = (List<Seller>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
			page.setPageData(sellers);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<Seller> getAllBySuperMarketId(long superMarketId){
		String sql = "from Seller where del!=1 and superMarket.id=? order by createDate desc";
		try {
			List<Seller> sellers = (List<Seller>)daoImpl.findListBySql(sql, new Object[]{superMarketId}, false, true, false, -1, -1);
			return sellers;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
